package com.leet.code.simple_array;

import java.util.HashSet;
import java.util.Set;

/**
 * @description:
 * @author: WYG
 * @time: 2020/11/29 10:26
 */

public class FairCandySwap {
    public static void main(String[] args) {
        if (true || false) {
            System.out.println(true);
        }
    }
    /**
     * ASum - x + y = BSum + x - y
     * we can get the following conclusion : y = x + (BSum - ASum) / 2;
     * x is A's element; y is B' element; ASum, BSum is the sum of A and B
     * if B contains y, x and y is result;
     * @param A
     * @param B
     * @return
     */
    public int[] fairCandySwap(int[] A, int[] B) {
        Set<Integer> Bob = new HashSet<>();
        int ASum = 0;
        int BSum = 0;
        for (int i = 0; i < A.length; i++) {
            ASum += A[i];
        }
        for (int j = 0; j < B.length; j++) {
            BSum += B[j];
            Bob.add(B[j]);
        }
        for (int i = 0; i < A.length; i++) {
            if (Bob.contains(A[i] + (BSum - ASum) / 2)) {
                return new int[]{A[i], A[i] + (BSum - ASum) / 2};
            }
        }
        return null;
    }
}
